package DBLayer;
import ModelLayer.*;

import java.sql.*;
import java.util.ArrayList;

public class DBSupplier {
	private Connection con;
	/**Creates new instance of DBSupplier*/
	public DBSupplier() {
		con = DbConnection1.getInstance().getDBcon();
	}
	
	public Supplier findSupplier(int suppId, boolean retriveAssociation) {
		String wClause = "  suppId = '" + suppId + "'";
		return singleWhere(wClause, retriveAssociation);
	}
	// get all Persons
	public ArrayList<Supplier> getAllSuppliers(boolean retriveAssociation)
	{
		return miscWhere("", retriveAssociation);
	}

	//Singlewhere is used when we only select one person 	
	private Supplier singleWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		Supplier supObj = new Supplier();

		String query =  buildQuery(wClause);
		System.out.println(query);
		try{ // read the supplier from the database
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			results = stmt.executeQuery(query);

			if( results.next() ){
				supObj = buildSupplier(results);
				//assocaition is to be build
				stmt.close();
				if(retrieveAssociation)
				{   
				}
			}
			else{ //no employee was found
				supObj = null;
			}
		}//end try	
		catch(Exception e){
			System.out.println("Query exception: "+e);
		}
		return supObj;
	}

	//find one employee having the name
	public Supplier searchSupplierName(String attValue, boolean retriveAssociation)
	{
		String wClause = "Name = '" + attValue + "'";
		System.out.println("SearchSupplier " + wClause);
		return singleWhere(wClause, retriveAssociation);
	}

	private ArrayList<Supplier> miscWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		ArrayList<Supplier> list = new ArrayList<Supplier>();

		String query = buildQuery(wClause);

		try{ //read the Supplier from database
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			results = stmt.executeQuery(query);

			while( results.next()){
				Supplier supObj = new Supplier();
				supObj = buildSupplier(results);
				list.add(supObj);
			}//end while
			stmt.close();
		}//end try
		catch(Exception e){
			System.out.println("Query exception - select: "+e);
			e.printStackTrace();
		}
		return list;
	}

	private String buildQuery(String wClause)
	{
		String query="SELECT * FROM Supplier";

		if (wClause.length()>0)
			query=query+ " WHERE"+ wClause;
		return query;
	}

	private Supplier buildSupplier(ResultSet results)
	{	Supplier supObj = new Supplier();
	try{// the columns from table Supplier are used
		supObj.setSuppId(results.getInt("suppId"));
		supObj.setName(results.getString("name"));
		supObj.setAddresse(results.getString("addresse"));
		supObj.setCountry(results.getString("country"));
		supObj.setPhone(results.getString("phone"));
		supObj.setEmail(results.getString("email"));

	}
	catch(Exception e)
	{
		System.out.println("error in building the person object");
	}
	return supObj;
	}
	
}